package com.doubao.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doubao.common.result.Result;
import com.doubao.device.dto.RatingDTO;
import com.doubao.device.dto.request.RatingRequest;
import com.doubao.device.service.DeviceRatingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/device/rating")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "智能体评分管理", description = "智能体评分相关接口")
public class DeviceRatingController {

    private final DeviceRatingService ratingService;

    @PostMapping
    @Operation(summary = "评分", description = "对智能体进行评分")
    public Result<RatingDTO> rateDevice(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Validated @RequestBody RatingRequest request) {
        return ratingService.rateDevice(userId, request);
    }

    @DeleteMapping("/{ratingId}")
    @Operation(summary = "删除评分", description = "删除指定的评分")
    public Result<Void> deleteRating(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Parameter(description = "评分ID") @PathVariable Long ratingId) {
        return ratingService.deleteRating(userId, ratingId);
    }

    @GetMapping("/user/{deviceId}")
    @Operation(summary = "获取用户对智能体的评分", description = "获取当前用户对指定智能体的评分")
    public Result<RatingDTO> getUserRating(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        return ratingService.getUserRating(userId, deviceId);
    }

    @GetMapping("/device/{deviceId}")
    @Operation(summary = "获取智能体评分列表", description = "获取指定智能体的评分列表")
    public Result<IPage<RatingDTO>> getDeviceRatings(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size) {
        return ratingService.getDeviceRatings(deviceId, page, size);
    }

    @GetMapping("/check/{deviceId}")
    @Operation(summary = "判断用户是否已评分", description = "判断当前用户是否已对指定智能体评分")
    public Result<Boolean> isRated(
            @Parameter(description = "用户ID") @RequestHeader(value = "X-User-ID", required = false) Long userId,
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        return ratingService.isRated(userId, deviceId);
    }

    @GetMapping("/average/{deviceId}")
    @Operation(summary = "获取智能体平均评分", description = "获取指定智能体的平均评分")
    public Result<BigDecimal> getAverageRating(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        return ratingService.getAverageRating(deviceId);
    }

    @GetMapping("/stats/{deviceId}")
    @Operation(summary = "获取智能体评分统计", description = "获取指定智能体的评分统计")
    public Result<Map<Integer, Integer>> getRatingStats(
            @Parameter(description = "智能体ID") @PathVariable Long deviceId) {
        return ratingService.getRatingStats(deviceId);
    }
} 